//
//  TopicDetailViewController.m
//  hospitalVIP
//
//  Created by xiaoyan on 14-3-9.
//  Copyright (c) 2014年 MEETStudio. All rights reserved.
//

#import "TopicDetailViewController.h"

@interface TopicDetailViewController (){
    
    NSMutableArray *commentsArray;
    NSMutableArray *cellheights;
    float contentheight;
}

@end

@implementation TopicDetailViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc{
    
    [commentsArray release];
    [cellheights release];
    
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    //ios7 优化
    
    self.title = @"话题详情";
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bg_pattern_wood" ofType:@"png"];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:path]];

    
    if( iOS7) {
        
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.navigationController.navigationBar.translucent = NO;
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
        
    }
    else{
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbar.png"] forBarMetrics:UIBarMetricsDefault];
    }
    
    
    UIButton *leftBt = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [leftBt setImage:[UIImage imageNamed:@"icon_return.png"] forState:UIControlStateNormal];
    [leftBt addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:leftBt];
    [self.navigationItem setLeftBarButtonItem:leftButton];
    [leftBt release];
    [leftButton release];
    
    
    if (cellheights == Nil) {
        cellheights = [[NSMutableArray alloc]init];
    }
    contentheight = 550;
}
- (void)backAction:(id)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    if (section == 0) {
        return 1;}
    else{
        return commentsArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
            
            cell.backgroundColor  = [UIColor clearColor];
            
            UILabel *titleLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 300, 30)];
            titleLable.textColor = [UIColor whiteColor];
            titleLable.font = [UIFont boldSystemFontOfSize:17];
            titleLable.text = @"孕妇六周睡姿注意事项";
            [cell.contentView addSubview:titleLable];
            [titleLable release];
            
            UILabel *contentLable = [[UILabel alloc]init];
            contentLable.textColor = [UIColor whiteColor];
            contentLable.font = [UIFont systemFontOfSize:14];
            contentLable.numberOfLines = 0;
            contentLable.lineBreakMode = NSLineBreakByWordWrapping;
            contentLable.text = @"怀孕后，很多孕妈妈还不是很了解孕妇的正确睡姿是哪种，所以容易造成孕期失眠，困意重重。孕妇的正确睡姿到底是怎样的呢？左侧卧，是孕妇的正确睡姿。孕妇正确睡姿，会使孕妇安静入睡。一般心脏在左边，所以睡觉时最好是右侧卧，因为这样可以减少对心脏的压迫。但孕妇恰好相反，因为随着怀孕时间变长，子宫不断增大，甚至占据了整个腹腔，这样会使临近的组织器官受到挤压，子宫不同程度地向右旋转，从而使保护子宫的韧带和系膜处于紧张状态，系膜中给子宫提供营养的血管也受到牵拉，会影响胎儿的氧气供给，容易使胎儿慢性缺氧。孕妇采取左侧睡姿，就可以减轻子宫的右旋转，缓解子宫供血不足，对胎儿生长发育和孕妇生产是有利的。";
            
            UIFont *font = [UIFont systemFontOfSize:14];
            CGSize size = CGSizeMake(300,2000);
            CGSize labelsize = [contentLable.text sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByWordWrapping];

            contentLable.frame = CGRectMake(10, 45, 300, labelsize.height);
            [cell.contentView addSubview:contentLable];
            [contentLable release];
            
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10,contentLable.frame.origin.y +labelsize.height +10, 300, 300)];
            imageView.clipsToBounds = YES;
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.image = [UIImage imageNamed:@"testicon7.jpg"];
            [cell.contentView addSubview:imageView];
            [imageView release];

            
        }
        
        // Configure the cell...
        
        return cell;

    }
    
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        return contentheight;
    }
    else if(indexPath.section == 1 && indexPath.row < cellheights.count)
        return [[cellheights objectAtIndex:indexPath.row] intValue];
    
    return 0;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];

    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
 
 */

@end
